package com.aaa.charge.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.aaa.charge.dao.CareHistoryDao;
import com.aaa.charge.dao.CareOrderDao;
import com.aaa.charge.dao.CareOrderItemDao;
import com.aaa.charge.dao.ChargeItemDao;
import com.aaa.charge.feign.MedicineFeign;
import com.aaa.charge.service.CareTuiService;
import com.aaa.charge.service.DisPensingService;
import com.aaa.entity.CareHistory;
import com.aaa.entity.CareOrder;
import com.aaa.entity.CareOrderItem;
import com.aaa.entity.OrderChargeItem;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pzs
 * @date 2023/3/30 18:51
 * @description CareTuiServiceImpl
 */
@Service
public class DisPensingServiceImpl implements DisPensingService {
    @Autowired
    private CareHistoryDao careHistoryDao;
    @Autowired
    private CareOrderDao careOrderDao;
    @Autowired
    private CareOrderItemDao careOrderItemDao;
    @Autowired
    private ChargeItemDao orderChargeItemDao;
    @Autowired
    private MedicineFeign medicineFeignl;

    @Override
    public Result<Map<String,Object>> findcareHistory(String regId){
        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("reg_id", regId);
        //new CareHistory实体获取一行数据
        CareHistory careHistory = careHistoryDao.selectOne(wrapper);
        if (careHistory==null){
            return new Result(200,"单号错误");
        }
        QueryWrapper<CareOrder> wrapper1=new QueryWrapper<>();
        wrapper1.eq("ch_id",careHistory.getChId());
        List<CareOrder> careOrders = careOrderDao.selectList(wrapper1);
        List<Map<String,Object>> list=new ArrayList<>();
        if(careOrders.size()>0){
            //遍历careOrders
            for (CareOrder careOrder : careOrders) {
                Map<String,Object> map1= BeanUtil.beanToMap(careOrder);
                QueryWrapper<CareOrderItem> wrapper2=new QueryWrapper<>();
                wrapper2.eq("status",1);
                wrapper2.eq("item_type",0);
                wrapper2.eq("co_id",careOrder.getCoId());
                List<CareOrderItem> careOrderItems = careOrderItemDao.selectList(wrapper2);
                if (careOrderItems.size()!=0) {
                    //计算订单金额
                    Double sum = careOrderItems.stream()
                            .map(item -> item.getAmount()
                                    .doubleValue()).reduce((a, b) -> a + b).get();
                    map1.put("careOrderItems",careOrderItems);
                    map1.put("sum",sum);
                    list.add(map1);
                }
            }
        }
        if(list.isEmpty()){
            return new Result<>(200,"请输入为支付的订单Id");
        }else {
            HashMap<String, Object> map = new HashMap<>();
            map.put("careHistory", careHistory);
            map.put("list" , list);
            return new Result<>(200, "成功", map);
        }

    }

    @Override
    public Result<Boolean> upDisPensing(String[] itemId) {
        //改变药品数量
        for (String s : itemId) {
            Boolean aBoolean = medicineFeignl.upSum(s);
            if (aBoolean){
                CareOrderItem careOrderItem=new CareOrderItem();
                careOrderItem.setStatus("3");
                QueryWrapper<CareOrderItem> wrapper= new QueryWrapper<>();
                wrapper.eq("item_id",s);
                careOrderItemDao.update(careOrderItem,wrapper);

                OrderChargeItem  orderChargeItem=new OrderChargeItem();
                orderChargeItem.setStatus("3");
                QueryWrapper<OrderChargeItem> wrapper1=new QueryWrapper<>();
                wrapper1.eq("item_id",s);
                orderChargeItemDao.update(orderChargeItem,wrapper1);
            }
        }
        return new Result<>(200,"已完成发药",true);

    }

}
